<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item v-if="admShow()" label="企业名称" prop="companyNo">
            <el-select v-model="formFilter.companyNo" @change="selectChange">
              <el-option 
                v-for="item in CompList"
                :key="item.companyNo"
                :label="item.companyName"
                :value="item.companyNo"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间" prop="startDate">
				     <div class="block">
							    <el-date-picker
							      v-model.trim="formFilter.startDate"
                    style="width:150px"
                    value-format="yyyy-MM-dd" 
                    @change="getSTime"
							      type="date"
							      placeholder="选择日期时间">
							    </el-date-picker>
							  </div>
				  </el-form-item>
		      <el-form-item label="结束时间" prop="endDate">
		         <div class="block">
						    <el-date-picker
						      v-model.trim="formFilter.endDate"
                  style="width:150px"
                  value-format="yyyy-MM-dd" 
                  @change="getETime"
						      type="date"
						      placeholder="选择日期时间">
						    </el-date-picker>
						  </div>
		      </el-form-item>
          <el-form-item>
            <el-button type="info" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
            <el-button @click="exportForm('formFilter')">批量导出</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
    </vue-header>
    <vue-body>
      <el-table
      	v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="companyName"
          label="企业名称">
        </el-table-column>
        <el-table-column
          prop="transAmts"
          label="交易金额">
        </el-table-column>
        <el-table-column
          prop="transIntegral"
          label="交易积分">
        </el-table-column>
        <el-table-column
          label="交易类型">
          <template slot-scope="scope">
            {{scope.row.transType | transType}}
          </template>
        </el-table-column>
        <el-table-column
          label="交易描述">
          <template slot-scope="scope">
            {{scope.row.transDesc || '无交易描述'}}
          </template>
        </el-table-column>
        <el-table-column
          label="交易状态">
          <template slot-scope="scope">
            {{scope.row.transStatus | transStatus}}
          </template>
        </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
  </vue-content>
</template>

<script>
  import { md5 } from '@/utilities/crypto' // 加密
  import {queryCompanyList} from '@/services/staffManager'
  import AuthStorage from '@/services/storage.js'
  import {compReport} from '@/services/reports'
  import {mapGetters} from 'vuex'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  export default {
    name: 'role-table',
    components: {},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
        companyName:"",
        startDate:"",
        endDate:""
      },
      CategoryList:[],
      loading: false,
      view: '',
      CompList:[],
      cascaderConfig: {
				label: 'categoryName',
				value: 'categoryNo',
				children: 'secondLevel'
      },
      category:[],
      ifSelect:true
    }),
    computed: {
      ...mapGetters(['token'])
    },
    methods: {
      admShow(){
        if(this.token.userType == '01'){
          return true
        }else{
          return false
        }
      },
      async loadCompList () {
        try {
          let data = await queryCompanyList();
          // this.formFilter.companyNo = data.data[0].companyNo;
          // console.log(data)
          this.CompList = data.data;
          this.loadData();
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async loadData () {
        this.loading = true
        try {
          let data = await compReport(Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter))
          // debugger
          this.tableData = data.data
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async exportData () {
        var param=Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter);
        var  a = new Date();
        var btime= a .getTime();
        var signs2 = "data="+JSON.stringify(param)+"&stmb="+btime;
        this.postDownLoadFile({
          url: this.$store.state.exportUrl + 'ecshop/company/companyAccountTransferExport.do?stmb='+ a.getTime() +'&source="03"&token='+ AuthStorage.token +'&version=""&data='+JSON.stringify(param)+'&sign='+this.signature(signs2),
          method:'post'
        });
      },
      selectChange(item){
        // console.log(item)
        this.formFilter.companyNo = item;
        this.loadData()
      },
      getSTime(val) {
          this.formFilter.startDate=val;//这个sTime是在data中声明的，也就是v-model绑定的值
      },
      getETime(val){
          this.formFilter.endDate=val;
      },
      signature(data) {
        return md5(data)
      },
      SearchSubmit () {
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      exportForm(){
        this.exportData();
      },
      postDownLoadFile(options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
              $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      transType (value) {
        return {"01": '充值', "02": '开支'}[value]
      },
      transStatus(value){
        return {"01": '交易中', "02": '正常',"03": '已撤销'}[value]
      }
    },
    created () {
      this.loadCompList();
    }
  }
</script>
